package de.fhkl.whatsstroke;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/** A basic Camera preview class to display a video while recording it. */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
{
	private SurfaceHolder mHolder;
	private Camera mCamera;
	
	public CameraPreview(Context context)
	{
		super(context);
	}

	public CameraPreview(Context context, AttributeSet set)
	{
		super(context, set);
	}
	
	@SuppressWarnings("deprecation")
	public CameraPreview(Context context, Camera camera)
	{
		super(context);
		mCamera = camera;

		// Install a SurfaceHolder.Callback so we get notified when the
		// underlying surface is created and destroyed.
		mHolder = getHolder();
		mHolder.addCallback(this);

		// deprecated setting, but required on Android versions prior to 3.0
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder)
	{
		// The Surface has been created, now tell the camera where to draw the
		// preview.
		Log.i("CameraPreview", "Create Surface");
		try
		{
			if (holder == null)
			{
				Log.i("CameraPreview", "Holder is null");
				return;
			}

			// setDisplayOrientation(mCamera, 90);
			mCamera.setPreviewDisplay(holder);
			mCamera.startPreview();
		}
		catch (IOException e)
		{
			Log.e("CameraPreview", "Error setting camera preview: " + e.getMessage());
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder)
	{
		// Stop preview in activity
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
	{
		// fixed landscape mode, no need to adjust the surfaceHolder
	}
}
